package com.akk.module.authentication;

import com.akk.exception.RestBusinessException;
import com.akk.module.authentication.model.request.AuthRequest;
import com.akk.module.authentication.model.request.IdCardAuthRequest;
import com.akk.module.authentication.model.response.AuthResponse;
import com.akk.module.authentication.model.response.IdCardAuthResponse;
import com.akk.module.authentication.utils.Base64;
import com.akk.module.upload.service.PictureService;
import com.akk.utils.Constants;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class IdCardAuthService extends AuthService {

    private static IdCardAuthService idCardAuthService;

    @Autowired
    private PictureService pictureService;

    private Logger logger = LoggerFactory.getLogger(IdCardAuthService.class);

    public static IdCardAuthService getInstance() {
        if(idCardAuthService == null)
            idCardAuthService = new IdCardAuthService();
        return idCardAuthService;
    }




    @Override
    public AuthResponse auth(AuthRequest request) {
        JSONObject content = new JSONObject();
        IdCardAuthRequest idCardAuthRequest =(IdCardAuthRequest)request;
        content.put("realName", idCardAuthRequest.getRealName());//姓名
        content.put("idCard", idCardAuthRequest.getIdCard());//身份证号码

        JSONObject json = getAuthMessage(Constants.AUTH_USER_CODE,
                Constants.AUTH_APP_CODE,idCardAuthRequest.getIdCardReason(),content);

        /*调用非头像接口*/
        JSONObject result;
        if (idCardAuthRequest.getTyped().equals(IdCardAuthRequest.TypedE.NO_PHOTO.getValue())) {
            result = sendEncMessage(json.toString(),
                    Constants.AUTH_IDCARD_URL,
                    Constants.AUTH_IDCARD_APP_KEY,
                    Constants.AUTH_VECTOR,
                    Constants.AUTH_USER_CODE,
                    Constants.AUTH_IDCARD_PRIVATE_KEY);
        }else{
            result = sendEncMessage(json.toString(),
                    Constants.AUTH_IDCARD_PIC_URL,
                    Constants.AUTH_IDCARD_PIC_APP_KEY,
                    Constants.AUTH_VECTOR,
                    Constants.AUTH_USER_CODE,
                    Constants.AUTH_IDCARD_PIC_PRIVATE_KEY);

        }
        if ( result.has("data")) {//正确报文
            JSONArray dataArray = result.getJSONArray("data");
            JSONObject dataObject = (JSONObject) dataArray.get(0);
            //获取照片photo
            JSONArray recordArray = dataObject.getJSONArray("record");
            JSONObject recordObject = (JSONObject) recordArray.get(0);

            if(recordObject.get("photo") != null) {
                String url = pictureService.uploadPictureByAuth(Base64.decode(recordObject.get("photo").toString()));
                recordObject.put("photo",url);
            }
            return (AuthResponse) JSONObject.toBean(recordObject, IdCardAuthResponse.class);
        }
        //错误报文
        JSONObject msgObject = result.getJSONObject("msg");
        logger.error("身份证实名认证出错.-->状态码:"+msgObject.get("code")+"/详情:"+msgObject.get("codeDesc"));
        throw new RestBusinessException("实名认证失败");
    }
}
